﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Exp.DAL
Imports Exp.Model


Public Class User_Service
    Private _objUser As New REM_User


    Public Sub New(objUser As REM_User)
        _objUser = objUser
    End Sub

    Public Sub New()

    End Sub

    Public Function GetAll() As List(Of EXP_Select_REM_User_Result)
        Dim ctx As New EXPEntities
        Dim list = ctx.EXP_Select_REM_User.ToList()
        Return list

    End Function

    Public Function GetByUserPss(ByVal User As String, ByVal PSS As String) As REM_User
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_User.Where(Function(o) o.UserName = User And o.Password = PSS).FirstOrDefault()
        Return obj
    End Function

    Public Function GetByID(ByVal ID As Integer) As REM_User
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_User.Where(Function(o) o.User_ID = ID).FirstOrDefault()
        Return obj


    End Function

    Public Function GetFullPoint(ByVal user_id As Integer) As Decimal

        Dim ctx As New EXPEntities
        Dim listUnit = ctx.REM_Owner.Where(Function(o) o.User_ID = user_id).ToList
        Dim icount As Integer = listUnit.Count
        Dim sumTotalUnitPoint As Decimal = 0
        Dim unitid As Integer

        Dim pointPerNight As Double = 0
        Dim qouta As Double = 0

        For i As Integer = 0 To icount - 1
            unitid = listUnit(i).Unit_ID
            qouta = ctx.REM_Owner.Where(Function(o) o.Unit_ID = unitid).FirstOrDefault.Quota
            pointPerNight = ctx.REM_Unit.Where(Function(o) o.Unit_ID = unitid).FirstOrDefault.PointPerNight
            sumTotalUnitPoint += (qouta * pointPerNight)
        Next

        Return sumTotalUnitPoint
    End Function

    Public Function GetPointBalance(ByVal user_id As Integer) As Decimal
        Dim BalancePoint As Decimal = GetFullPoint(user_id)

        Dim ctx As New EXPEntities

        Dim listUnitApproved = ctx.RET_Booking.Where(Function(o) o.Requester_ID = user_id).ToList
        Dim icount As Integer = listUnitApproved.Count
        Dim sumTotalUsedPoint As Decimal = 0

        Dim Booking_ID As Integer
        For i As Integer = 0 To icount - 1
            Booking_ID = listUnitApproved(i).Booking_ID
            Dim list = ctx.RET_Booking_Unit.Where(Function(o) o.Booking_ID = Booking_ID).ToList()
            For j As Integer = 0 To list.Count - 1
                sumTotalUsedPoint += list(0).Point
            Next

        Next

        BalancePoint = BalancePoint - sumTotalUsedPoint

        Return BalancePoint
    End Function

    Public Sub Add()
        Dim ctx As New EXPEntities
        ctx.REM_User.AddObject(_objUser)
        ctx.SaveChanges()
    End Sub

    Public Sub Edit()
        Try
            Dim ctx As New EXPEntities
            Dim obj = ctx.REM_User.Where(Function(o) o.User_ID = _objUser.User_ID).FirstOrDefault()
            obj.UserName = _objUser.UserName
            obj.Role_ID = _objUser.Role_ID
            obj.TitleName = _objUser.TitleName
            obj.FirstName = _objUser.FirstName
            obj.LastName = _objUser.LastName
            obj.MobilePhone = _objUser.MobilePhone
            obj.HomePhone = _objUser.HomePhone
            obj.Email = _objUser.Email
            obj.CreditcardNumber = _objUser.CreditcardNumber
            obj.Address = _objUser.Address
            If _objUser.Role_ID = 3 Then
                obj.JoinDate = _objUser.JoinDate
                obj.ExpireDate = _objUser.ExpireDate
            End If

            obj.isActive = _objUser.isActive
            obj.UpdateUser = _objUser.UpdateUser
            obj.UpdateDate = Now

            ctx.SaveChanges()
        Catch ex As Exception

        End Try


    End Sub

    Public Sub UpdateLogin(ByVal User_ID As Integer)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_User.Where(Function(o) o.User_ID = User_ID).FirstOrDefault()
        obj.LastLoginDate = Now
        ctx.SaveChanges()
    End Sub

    Public Sub Delete(ByVal ID As Integer)
        Dim ctx As New EXPEntities
        Dim obj = ctx.REM_User.Where(Function(o) o.User_ID = ID).FirstOrDefault()
        ctx.REM_User.DeleteObject(obj)
        ctx.SaveChanges()
    End Sub

    Public Function isHasRight(ByVal User_ID As Integer, ByVal MenuName As String) As Boolean
        Dim Result As Boolean = False
        Dim ctx As New EXPEntities
        Dim RoleID = ctx.REM_User.Where(Function(o) o.User_ID = User_ID).FirstOrDefault.Role_ID
        Dim iCount = ctx.REM_RoleMenu.Where(Function(o) o.Role_ID = RoleID And o.MenuName = MenuName).Count
        If iCount = 1 Then Result = True
        Return Result

    End Function

    Public Function getOwnerDetail(ByVal UserId As Integer) As List(Of EXP_Owner_detail)
        Dim ctx As New EXPEntities
        Return ctx.EXP_Get_Owner_detail(UserId).ToList()
    End Function

    Public Function GetListApprover() As List(Of REM_User)
        Dim ctx As New EXPEntities
        Dim list = ctx.REM_User.Where(Function(o) o.Role_ID = 2).OrderBy(Function(o) o.Email).ToList

        Return list
    End Function

    Public Function UpdatePassword(ByVal uid As Integer, ByVal NewPwd As String) As Boolean
        Dim ctx As New EXPEntities
        Dim u As REM_User = ctx.REM_User.Where(Function(o) o.User_ID = uid).FirstOrDefault()

        If u IsNot Nothing Then
            u.Password = NewPwd
            ctx.SaveChanges()

            Return True
        End If

        Return False
    End Function

    Public Function GetUsagePoint(ByVal UserId As Integer) As Decimal
        Dim ctx As New EXPEntities
        Dim a = ctx.EXP_Get_Usage_point(UserId).First
        Return If(a IsNot Nothing, a.Value, 0)
    End Function
End Class


